package com.jatosoft.poc.jsf.managedbean;

import javax.annotation.PostConstruct;

import com.jatosoft.poc.priceinquiry.core.domain.Article;
import com.jatosoft.poc.priceinquiry.core.domain.PriceRecord;
import com.jatosoft.poc.priceinquiry.core.service.ArticlePriceService;

public class PriceMngBean extends BaseManagedBean {

	private static final long serialVersionUID = -9027209733443238589L;

	@PostConstruct
	public void init() {
		logger.debug("init");
		for(int i=0;i<10;i++){
			article.addPriceRecord(new PriceRecord());
		}
		queryArticle();

	}

	public String saveArticle() {
		logger.debug("saveArticle");

		articlePriceService.addAtricle(article);
		return queryArticle();
	}

	public String addArticle() {
		return "ARTICLE_ADD";
	}

	public String queryArticle() {
		logger.debug("queryArticle");
		dumpModel2UIModel(articlePriceService.listArticle());
		return "ARTICLE_LIST";
	}

	public String viewArticle() {
		articleId = getFacesContext().getExternalContext()
				.getRequestParameterMap().get("articleId");
		logger.debug("articleId:\t" + articleId);
		article = articlePriceService.getArticleById(articleId);
		article.addPriceRecord(new PriceRecord());
		return "ARTICLE_VIEW";
	}
	
	public String removePrice(){
		String priceId = getFacesContext().getExternalContext()
				.getRequestParameterMap().get("priceRecordId");
		logger.debug("priceRecordId:\t" + priceId);
		
		return "ARTICLE_VIEW";
	}

	ArticlePriceService articlePriceService;

	Article article = new Article();
	String articleId;

	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	public void setArticlePriceService(ArticlePriceService articlePriceService) {
		this.articlePriceService = articlePriceService;
	}

	public String getArticleId() {
		return articleId;
	}

	public void setArticleId(String articleId) {
		this.articleId = articleId;
	}
}
